package com.luyou.a04;

import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.core.env.StandardEnvironment;

/**
 *
 * 测试AutowiredAnnotationBeanPostProcessor
 * 解析@Autowired @Value
 * @author 28336
 * @version 1.0
 * Create by 2023/12/12 8:30
 */
public class DigInAutowired {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 注册成品 Bean，不再进行 Bean 的创建、依赖注入、初始化等操作
        beanFactory.registerSingleton("bean2", new Bean2());
        beanFactory.registerSingleton("bean3", new Bean3());

        // @Value
        beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
        // #{}
        beanFactory.addEmbeddedValueResolver(new StandardEnvironment()::resolvePlaceholders);

        // 查看哪些属性、方法加了 @Autowired，这称之为 InjectionMetadata
        AutowiredAnnotationBeanPostProcessor postProcessor = new AutowiredAnnotationBeanPostProcessor();
        postProcessor.setBeanFactory(beanFactory);


        Bean1 bean1 = new Bean1();
        System.out.println(bean1);
        /**
         * 执行依赖注入，@Autowired、@Value
         * findAutowiringMetadata(beanName, bean.getClass(), pvs) 获取类上元信息，injectedElements中存储被扫描到需要依赖注入的成员变量
         */
        postProcessor.postProcessProperties(null, bean1, "bean1");
        System.out.println(bean1);
    }
}
